package jblip.gui.components.view;

import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
import javax.swing.event.HyperlinkEvent.EventType;

import jblip.gui.data.FormattedUpdate;
import jblip.util.UpdateTag;

class TooltipHyperlinkListener implements HyperlinkListener {
  private final FormattedUpdate update;
  
  public TooltipHyperlinkListener(final FormattedUpdate formatted_update) {
    this.update = formatted_update;
  }

  @Override
  public void hyperlinkUpdate(final HyperlinkEvent e) {    
    if (e.getEventType() == EventType.ENTERED) {
      displayTooltip(getHumanReadableDescription(e.getDescription()),
          e.getSource());
    } else if (e.getEventType() == EventType.EXITED) {
      displayTooltip(null, e.getSource());
    }
  }

  private void displayTooltip(final String description, final Object source) {
    if (source instanceof TextViewPane) {
      TextViewPane tvp = (TextViewPane) source;
      tvp.setToolTipText(description);
    }
  }
  
  private String getHumanReadableDescription(final String description) {
    UpdateTag tag = update.getTag(description);
    if (tag != null) {
      return tag.getUserReadableString();
    } else {
      return null;
    }
  }
}
